package com.hzcc.hzccblog.module.controller;

import com.alibaba.druid.wall.violation.ErrorCode;
import com.baomidou.mybatisplus.extension.api.R;
import com.hzcc.hzccblog.common.Result;
import com.hzcc.hzccblog.common.enums.LoginEnums;
import com.hzcc.hzccblog.common.enums.PublicEnums;
import com.hzcc.hzccblog.module.entity.dto.RegisterDto;
import com.hzcc.hzccblog.module.entity.dto.UserLoginDto;
import com.hzcc.hzccblog.module.entity.vo.LoginUser;
import com.hzcc.hzccblog.module.service.LoginService;
import com.hzcc.hzccblog.module.service.MsmService;
import com.hzcc.hzccblog.module.service.UserService;
import com.hzcc.hzccblog.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.hzcc.hzccblog.common.Constant.LOGIN_TOKEN;

/**
 * @Author 早睡蛋
 * @Date 2022/7/11 21:11:11
 * @Desc:登录控制层
 */
@RestController
@RequestMapping("api/login")
@CrossOrigin
@Slf4j
@Api(tags = "登录注册相关")
public class LoginController {

    @Autowired
    private MsmService msmService;

    @Autowired
    private LoginService loginService;

    @Resource
    private RedisUtil redisUtil;


    @GetMapping("/loginUrl")
    public Result loginUrl(){
        return LoginEnums.PLEASE_LOGIN.getResult();
    }


    /**
     * shiro登录
     * @param userLoginDto
     * @return
     */
    @ApiOperation("shiro登录")
    @PostMapping("/login")
    public Result login(@RequestBody UserLoginDto userLoginDto,
                        HttpServletRequest request,
                        HttpServletResponse response){
        return loginService.userLogin(userLoginDto);
    }


    /**
     * 注册获取验证码
     * @param userEmail
     * @return
     */
    @ApiOperation("用户注册验证码获取")
    @GetMapping("/send/{userEmail}")
    @ResponseBody
    public Result getCode(@PathVariable String userEmail){
        return msmService.sendEmail(userEmail);
    }

    /**
     * 用户注册
     * @param registerDto
     * @return
     */
    @ApiOperation("用户注册")
    @PostMapping("/userRegister")
    public Result userRegister(@RequestBody RegisterDto registerDto){
        return loginService.userRegister(registerDto);
    }

    /**
     * 用户登录
     * @param userLoginDto
     * @return
     */
    @ApiOperation("用户登录")
    @PostMapping("/userLogin")
    public Result userLogin(@RequestBody UserLoginDto userLoginDto){
        return loginService.userLogin(userLoginDto);
    }

    @ApiOperation("登出")
    @GetMapping("/userLogout")
    public Result logout(HttpServletRequest request){
        return loginService.userLogout(request);
    }

}
